#include "g_headers.h"
#include "reg_win.h"
#include <string>
#include <QErrorMessage>
#include <QMessageBox>
#include <stdio.h>
#include <QMouseEvent>
#include <iostream>
using std::string;
using std::cout;
using std::endl;

Reg_Dialog::Reg_Dialog(Watcher * ptr, QDialog *parent)
{
    //隐藏窗口边框
    this->setWindowFlags(Qt::FramelessWindowHint);

    //窗体透明化
    this->setAttribute(Qt::WA_TranslucentBackground, true);

    //初始化窗体
    this->setupUi(this);

    //设置两个按钮透明
    submit_button->setFlat(true);

    gsyncer = ptr;

}

void Reg_Dialog::mousePressEvent(QMouseEvent * event)
{
    if (event->button() == Qt::LeftButton) //点击左边鼠标
    {
         dragPosition = event->globalPos() - frameGeometry().topLeft();
         //globalPos()获取根窗口的相对路径，frameGeometry().topLeft()获取主窗口左上角的位置
         event->accept();   //鼠标事件被系统接收
    }
    if (event->button() == Qt::RightButton)
    {
         close();
    }
}

void Reg_Dialog::mouseMoveEvent(QMouseEvent * event)
{
    if (event->buttons() == Qt::LeftButton) //当满足鼠标左键被点击时。
    {
         move(event->globalPos() - dragPosition);//移动窗口
         event->accept();
    }
}

void Reg_Dialog::on_submit_button_clicked()
{
    if (connect_server(gsyncer->ctl_sock,gsyncer->srv_ctl) < 0)
        return;

    QString q_usr = usr_input->text();
    QString q_pwd_1 = pwd_input_1->text();
    QString q_pwd_2 = pwd_input_2->text();

    string usr = q_usr.toStdString();
    string pwd_1 = q_pwd_1.toStdString();
    string pwd_2 = q_pwd_2.toStdString();

    //检验两次密码是否相同
    if( pwd_1 != pwd_2 )
    {
        QErrorMessage err_msg(this);
        err_msg.setWindowTitle("Error:");
        err_msg.showMessage("the two passwords are not same!");
        err_msg.exec();

        ::close(gsyncer->ctl_sock);
        return;
    }

    //打包
    ctl_pkg_t info;
    info.type = REG;
    strncpy(info.str.id.usr,usr.c_str(),USR_LEN-1);
    strncpy(info.str.id.pwd,pwd_1.c_str(),PWD_LEN-1);

    cout << info.str.id.usr << " " << info.str.id.pwd << endl;

    //发送给服务器
    if( writen(gsyncer->ctl_sock,&info,sizeof(ctl_pkg_t)) < 0 )
    {
        perror("write");
        QErrorMessage err_msg(this);
        err_msg.setWindowTitle("Error:");
        err_msg.showMessage("can't write to the server");
        err_msg.exec();

        ::close(gsyncer->ctl_sock);
        return;
    }

    //读取服务器的应答
    int8_t reply;
    if( readn(gsyncer->ctl_sock,&reply,sizeof(reply)) < 0 )
    {
        QErrorMessage err_msg(this);
        err_msg.setWindowTitle("Error:");
        err_msg.showMessage("can't read from the server");
        err_msg.exec();

        ::close(gsyncer->ctl_sock);
        return;
    }

    if( reply == -1 )
    {
        QErrorMessage err_msg(this);
        err_msg.setWindowTitle("Error:");
        err_msg.showMessage("Your username has been registrated.. Please try again.");
        err_msg.exec();

        ::close(gsyncer->ctl_sock);
        return;
    }

    QMessageBox::about(NULL, "Congratulations!", "Registration succeed!");
    this->close();
}
